package proxy;

import java.util.Date;
import java.util.HashMap;
/**
 * This class is responsable of managing the content inside the cache.
 * 
 * @author swordfish
 */
public class CacheManager {
	private static CacheManager singleton = new CacheManager();
	public HashMap<String, CacheEntry> cacheEntries = new HashMap<String, CacheEntry>();
	
	private CacheManager(){}
	
	/**
	 * Return true if the cache contains the given entry. False otherwise.
	 * @param key
	 * @return boolean
	 */
	public boolean exists(String key){
		if(cacheEntries.containsKey(key)){
			CacheEntry entry = cacheEntries.get(key);
			Date nowTime = new Date();
			if(nowTime.before(entry.getExpirationDate())){
				return true;
			}
		}
		return false;
	}
	
	public CacheEntry getEntry(String key){
		return cacheEntries.get(key);
	}
	
	public void storeEntry(String key, CacheEntry entry){
		cacheEntries.put(key, entry);
	}
	
	public void flush(){
		cacheEntries.clear();
	}
	
	public static CacheManager getCacheManager(){
		return singleton;
	}
}
